package com.smallrig.mall.template.huawei;

public class 光伏基地 {

    //2 5 2(正方形) 6(发电量)
    //1 3 4 5 8
    //2 3 6 7 1
    static int[][] jidi = new int[][]{{1,3,4,5,8},{2,3,6,7,1}};
    static int w = 2;
    static int val = 6;

    public static void main(String[] args) {
        System.out.println(solvePrefix());
    }

    public static int solveFor4(){
        int cnt = 0;
        for(int i=0;i<=jidi.length-w;i++){
            for(int j=0;j<=jidi[0].length-w;j++){
                int ans = 0;
                for(int m=i;m<i+w;m++){
                    for(int n=j;n<j+w;n++){
                        ans += jidi[m][n];
                    }
                }
                if(ans>=val){
                    cnt++;
                }
            }
        }
        return cnt;
    }

    //二维前缀和
    public static int solvePrefix(){
        int cnt = 0;

        int[][] sum = new int[jidi.length+1][jidi[0].length+1];

        for(int i=0;i<jidi.length;i++){
            for(int j=0;j<jidi[0].length;j++) {
                sum[i+1][j+1] = jidi[i][j] + sum[i+1][j] + sum[i][j+1] - sum[i][j];
            }
        }

        for(int i=0;i<=jidi.length-w;i++){
            for(int j=0;j<=jidi[0].length-w;j++) {

                int ans = sum[i+w][j+w] -sum[i+w][j] - sum[i][j+w] +sum[i][j];

                if(ans>=val){
                    cnt++;
                }
            }
        }
        return cnt;
    }

}
